package cn.tedu.collection;/**
 * @Author 金帮嘉诚 @Date 2021-06-21 星期一 16:46
 */
/*本类用于测试Set接口*/

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * 知识点
 * HashSet底层是HashMap
 */
public class TestSet {
    public static void main(String[] args) {
        Set<String> set = new HashSet<>();
        set.add("牛气冲天");
        set.add("牛气冲天");
        set.add("牛气冲天");
        set.add("虎虎生威");
        System.out.println(set);

        /*1.set集合中的元素都是无序的
         * 2.set集合当中的元素都不可以重复,可以存null值
         * 3.set集合中可以存放null值，也只允许存0-1个*/
        set.add(null);
        set.add("null");
        System.out.println(set);
        System.out.println();

//        set.clear();
        //
        System.out.println("set.contains(\"小老虎\") = " + set.contains("小老虎"));
        System.out.println("set.hashCode() = " + set.hashCode());
        System.out.println("set.isEmpty() = " + set.isEmpty());
        System.out.println("set.size() = " + set.size());

        System.out.println("set.equals(\"虎虎生威\") = " + set.equals("虎虎生威"));
        System.out.println("set.remove(\"null\") = " + set.remove("null"));
        System.out.println("set.remove(null) = " + set.remove(null));

        Object[] array = set.toArray();
        System.out.println("Arrays.toString(array) = " + Arrays.toString(array));
        System.out.println();
        //
        Set<String> set1 = new HashSet<>();

        set1.add("小狮几");
        set1.add("小海藤");
        set1.add("小兔纸");
        set1.add("小脑斧");
        System.out.println("set1 = " + set1);
        System.out.println("set.addAll(set1) = " + set.addAll(set1));
        System.out.println("set = " + set);
        System.out.println("set.containsAll(set1) = " + set.containsAll(set1));
//        System.out.println("set.retainAll(set1) = " + set.retainAll(set1));
//        System.out.println("set = " + set);
//        System.out.println("set1 = " + set1);
        System.out.println();
        /*retainAll是取两个集合的交集【公共部分】
         * 需要注意的是，谁来调用就影响谁*/
        System.out.println("set1. retainAll(set) = " + set1.retainAll(set));//交集
        System.out.println("set  = " + set);
        System.out.println("set1 = " + set1);


        //1.
        Iterator<String> it = set.iterator();
        //2.
        while (it.hasNext()) {
            //3.
            System.out.println(it.next());
        }

    }
}
